package com.mb.app.controller;

import com.mb.app.domain.*;
import com.mb.app.service.ITbProductInfoService;
import com.mb.app.service.ITbSpecialClassService;
import com.mb.app.service.ITbSpecialProductInfoService;
import com.mb.app.service.ITbSpecialTopicService;
import com.mb.common.config.CommonConfig;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 订单表  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-15
 */
@Controller
@RequestMapping("/app/special")
@Slf4j
public class TbSpecialController extends BaseController {

    @Autowired
    private ITbSpecialTopicService specialTopicService;

    @Autowired
    private ITbSpecialClassService specialClassService;

    @Autowired
    private ITbSpecialProductInfoService specialProductInfoService;

    @Autowired
    private  ITbProductInfoService productInfoService;

    @Autowired
    private CommonConfig commonConfig;

    @GetMapping("/topic")
    String index(){
        return "/app/special/special_list";
    }

    /**
     * 专题信息
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductInfo> list = specialTopicService.queryList(query);
        int total = specialTopicService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    /**
     * 跳转添加专题页面
     * @param
     * @return
     */
    @GetMapping("/add")
    String add(Model model){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data_status",1);
        List<TbSpecialClass> specialClasses = specialClassService.selectByMap(map);
        model.addAttribute("specialClasses",specialClasses);
        return "/app/special/special_add";
    }

    /**
     * 添加
     * @param specialTopic
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(TbSpecialTopic specialTopic){
        try {
            //切割图片地址字符串
            if (StringUtils.isNotEmpty(specialTopic.getPicUrl())) {
                String PicUrl = specialTopic.getPicUrl();
                PicUrl = PicUrl.replaceAll(commonConfig.getImgDomain(), "");
                specialTopic.setPicUrl(PicUrl);
            }
            boolean insert = specialTopicService.insert(specialTopic);
            if (insert){
                return R.ok();
            }

        } catch (Exception e) {
            log.error("update Exception", e);
        }
        return R.error("添加失败");
    }

    /**
     * 修改
     * @param specialTopic
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public R update(TbSpecialTopic specialTopic){
        try {
            //切割图片地址字符串
            if (StringUtils.isNotEmpty(specialTopic.getPicUrl())) {
                String PicUrl = specialTopic.getPicUrl();
                PicUrl = PicUrl.replaceAll(commonConfig.getImgDomain(), "");
                specialTopic.setPicUrl(PicUrl);
            }
            specialTopic.setUpdateTime(new Date());
            boolean b = specialTopicService.updateById(specialTopic);
            if (b){
                return R.ok();
            }
        } catch (Exception e) {
            log.error("update Exception", e);
        }
        return R.error("修改失败");
    }

    /**
     * 修改
     * @param specialTopicId
     * @param model
     * @return
     */
    @GetMapping("/edit/{specialTopicId}")
    String edit(@PathVariable("specialTopicId") String  specialTopicId, Model model){
        TbSpecialTopic tbSpecialTopic = specialTopicService.selectById(specialTopicId);
        tbSpecialTopic.setPicUrl(commonConfig.getImgDomain()+tbSpecialTopic.getPicUrl());
        HashMap<String, Object> map = new HashMap<>();
        map.put("data_status",1);
        List<TbSpecialClass> specialClasses = specialClassService.selectByMap(map);
        model.addAttribute("specialClasses",specialClasses);
        model.addAttribute("tbSpecialTopic",tbSpecialTopic);
        return "/app/special/special_edit";
    }


    /**
     * 删除
     * @param specialTopicId
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("specialTopicId") Long specialTopicId, HttpServletRequest request){
        TbSpecialTopic tbSpecialTopic = new TbSpecialTopic();
        tbSpecialTopic.setSpecialTopicId(specialTopicId);
        tbSpecialTopic.setDataStatus(0);
        boolean b = specialTopicService.updateById(tbSpecialTopic);
        if (b){
            return R.ok();
        }
        return R.error("删除失败");
    }


    @GetMapping("/product/{specialTopicId}")
    String product(@PathVariable("specialTopicId") String  specialTopicId, Model model){
        model.addAttribute("specialTopicId",specialTopicId);
        return "/app/special/special_products";
    }

    @ResponseBody
    @GetMapping("/products")
    public PageUtils products(@RequestParam Map<String, Object> params){
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbSpecialProductInfo> list = specialProductInfoService.queryList(query);
        int total = specialProductInfoService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    /**
     * 跳转添加专题商品页面
     * @param
     * @return
     */
    @GetMapping("/addProduct/{specialTopicId}")
    String addProduct(@PathVariable("specialTopicId") String  specialTopicId,Model model){
        model.addAttribute("specialTopicId",specialTopicId);
        return "/app/special/special_addProduct";
    }

    /**
     * 添加
     * @param specialProductInfo
     * @return
     */
    @ResponseBody
    @PostMapping("/saveProduct")
    public R saveProduct(TbSpecialProductInfo specialProductInfo){
        if (specialProductInfo.getProductIds().length == 0){
            specialProductInfo.setAddUserId(getUserId());
            specialProductInfoService.insert(specialProductInfo);
            return R.ok();
        }else {
            List<TbSpecialProductInfo> specialProductInfos = new ArrayList<>();
            for (Long productId:specialProductInfo.getProductIds()) {
                TbSpecialProductInfo specialProductInfo1 = new TbSpecialProductInfo();
                specialProductInfo1.setSpecialTopicId(specialProductInfo.getSpecialTopicId());
                specialProductInfo1.setAddUserId(getUserId());
                specialProductInfo1.setProductId(productId);
                specialProductInfos.add(specialProductInfo1);
            }
            specialProductInfoService.insertBatch(specialProductInfos);
            return R.ok();
        }
    }

    /**
     * 修改
     * @param specialTopic
     * @return
     */
    @ResponseBody
    @PostMapping("/updateProduct")
    public R updateProduct(TbSpecialTopic specialTopic){
        boolean b = specialTopicService.updateById(specialTopic);
        if (b){
            return R.ok();
        }
        return R.error("修改失败");
    }


    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/removeProduct")
    public R removeProduct(@RequestParam("id") Long id, HttpServletRequest request){
        TbSpecialProductInfo tbSpecialProductInfo = new TbSpecialProductInfo();
        tbSpecialProductInfo.setId(id);
        tbSpecialProductInfo.setDataStatus(0);
        boolean b = specialProductInfoService.updateById(tbSpecialProductInfo);
        if (b){
            return R.ok();
        }
        return R.error("删除失败");
    }


}
